﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Diagnostics;

namespace XbdSoft.WPFVersion.AutoUpdate
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public const string ExeProcessName = "小布叮软件.exe";
        protected string ExeInstallUrl = string.Empty;

        protected override void OnStartup(StartupEventArgs e)
        {
            //if (e.Args == null || e.Args.Length < 1 || e.Args[0].Length < 1)
            //{
            //    MessageBox.Show("自动更新程序参数不正确！");
            //    Process.GetCurrentProcess().Kill();
            //}

            //ExeInstallUrl = e.Args[0];
            ExeInstallUrl = "http://service.xbd61.com/update/Setup1.0.0.2.zip";

            var processes = Process.GetProcessesByName(ExeProcessName);
            if (processes != null && processes.Length > 0)
            {
                if (MessageBox.Show(string.Format("自动更新程序需要强制关闭正在运行的:{0}", ExeProcessName)) == MessageBoxResult.OK)
                {
                    foreach (var p in processes)
                    {
                        p.Kill();
                    }
                }
                else
                {
                    Process.GetCurrentProcess().Kill();
                }
            }
            base.OnStartup(e);
        }
        protected override void OnActivated(EventArgs e)
        {
            if (MainWindow != null)
            {
                (MainWindow as MainWindow).ExeInstallUrl = ExeInstallUrl;
            }
            base.OnActivated(e);
        }
         
    }
}
